import {Controller, BusinessError, ApiResponse, log} from "@codefree/badger/service";
import Auth from '../middleware/auth'
import dateFormat from 'dateformat'

const logger = log.getLogger('yunchou');

/**
 - 继承Controller,在controller中可以使用
 - this.ctx.config
 - this.service
 - action,参数(arguments):
 - req:request，url，body
 - res:response，返回请求数据
 - next:不阻塞进程
 - ApiResponse:
 - ok
 - bizException:业务异常
 - error
 - notFound
 */
//@Auth
export default class schedule extends Controller {
    async create() {
        const [req, res, next] = arguments;
        try {
            /**
             * key
             * value
             * description
             */

            let createBody = req.body;
            let fillArr=new Array();
            for(let i=0;i<createBody.fillResult.length;i++){
                fillArr[i]=createBody.fillResult[i].join('@');
            }
            let fillString=fillArr.join(',');
            let judgeArr=new Array();
            for(let i=0;i<createBody.judgeResult.length;i++){
                judgeArr[i]=createBody.judgeResult[i].join('@');
            }
            let judgeString=judgeArr.join(',');
            let selectionArr=new Array();
            for(let i=0;i<createBody.selectionResult.length;i++){
                selectionArr[i]=createBody.selectionResult[i].join('@');
            }
            let selectionString=selectionArr.join(',');
            let calculationArr=new Array();
            for(let i=0;i<createBody.calculationResult.length;i++){
                calculationArr[i]=createBody.calculationResult[i].join('@');
            }
            let calculationString=calculationArr.join(',');
            let applicationArr=new Array();
            for(let i=0;i<createBody.applicationResult.length;i++){
                applicationArr[i]=createBody.applicationResult[i].join('@');
            }
            let applicationString=applicationArr.join(',');
            let scheduleResult=new Array();
            scheduleResult.id=createBody.id;
            scheduleResult.schedule_start=createBody.start;
            scheduleResult.schedule_end=createBody.end;
            scheduleResult.prac_dline=createBody.prac;
            scheduleResult.test_dline=createBody.test;
            scheduleResult.schedule_scores=fillString+';'+judgeString+';'+selectionString+';'+calculationString+';'+applicationString;

            // logger.debug(`${this.constructor.name}.create:${JSON.stringify(createBody)}`)
             const createdData = await this.service.schedule.updateDescription(scheduleResult);
            res.send(ApiResponse.ok(createdData));
        } catch (err) {
            next(err);
        }
    }

    // async modify() {
    //     const [req, res, next] = arguments;
    //     try {
    //         let updateBody = req.body;
    //         await this.service.dictionary.updateDescription(updateBody.id, updateBody.description);
    //         res.send(ApiResponse.ok());
    //     } catch (err) {
    //         next(err);
    //     }
    // }

    async query() {
        const [req, res, next] = arguments;
        try {
            const data = await this.to(this.service.schedule.selfQuery({id:1}), next);
            if (data) res.send(ApiResponse.ok(data));
        } catch (err) {
            next(err);
        }
    }

    async queryClass(){
        const [req, res, next] = arguments;
        try {
           
            const listData = await this.to(this.service.schedule.queryClass({id:1}), next);

            if (listData) res.send(ApiResponse.ok(listData));
        } catch (err) {
            next(err);
        }
    }

    async queryScore(){
        const [req, res, next] = arguments;
        try {
            let createBody = req.body;
        
            const data = await this.to(this.service.schedule.queryScore(req.body.id),next);
            if(data.list[0].schedule_scores!==''){
                let schedule_scores=data.list[0].schedule_scores;
                let schedule_scores_arr=schedule_scores.split(";");
               
                for( let i=0;i<schedule_scores_arr.length;i++){
                    if(schedule_scores_arr[i].length > 0){
                    schedule_scores_arr[i]=schedule_scores_arr[i].split(",");
                    }else{
                        schedule_scores_arr[i]=[];
                    }
                }
                
                for( let j=0;j<schedule_scores_arr.length;j++){
                    
                    for(let z=0;z<schedule_scores_arr[j].length;z++){
                        schedule_scores_arr[j][z]=schedule_scores_arr[j][z].split("@");
                    }
               
                }

                if(schedule_scores_arr[0].length==0){
                    data.list[0].fillRowScore='1';
                    data.list[0].fillCountScore='0';
                }else{
                    data.list[0].fillRowScore=schedule_scores_arr[0][0][2]; 
                    let fillCountScoreArr=new Array();
                    for(let i=0;i<schedule_scores_arr[0].length;i++){
                        fillCountScoreArr[i]=schedule_scores_arr[0][i][2];
                    }
                    let fillSum = 0;
          
                    for (let i = 0; i < fillCountScoreArr.length; i++){
                        fillSum += parseInt(fillCountScoreArr[i]);
                        }
                   data.list[0].fillCountScore=fillSum;

                }
                if(schedule_scores_arr[1].length==0){
                    data.list[0].judgeRowScore='1';
                    data.list[0].judegCountScore='0';
                }else{
                    data.list[0].judgeRowScore=schedule_scores_arr[1][0][2]; 
                    let judgeCountScoreArr=new Array();
                    for(let i=0;i<schedule_scores_arr[1].length;i++){
                        judgeCountScoreArr[i]=schedule_scores_arr[1][i][2];
                    }
                    let selectionSum = 0;
          
                    for (let i = 0; i < judgeCountScoreArr.length; i++){
                        selectionSum += parseInt(judgeCountScoreArr[i]);
                        }
                   data.list[0].judgeCountScore=selectionSum;
                }
                if(schedule_scores_arr[2].length==0){
                    data.list[0].selectionRowScore='1';
                    data.list[0].selectionCountScore='0';
                }else{
                    data.list[0].selectionRowScore=schedule_scores_arr[2][0][2]; 
                    let selectionCountScoreArr=new Array();
                    for(let i=0;i<schedule_scores_arr[2].length;i++){
                        selectionCountScoreArr[i]=schedule_scores_arr[2][i][2];
                    }
                    let selectionSum = 0;
          
                    for (let i = 0; i < selectionCountScoreArr.length; i++){
                        selectionSum += parseInt(selectionCountScoreArr[i]);
                        }
                    data.list[0].selectionCountScore=selectionSum;
                }
                if(schedule_scores_arr[3].length==0){
                    data.list[0].calculationCountScore='0';
                }else{
                 
                    let calculationCountScoreArr=new Array();
                    for(let i=0;i<schedule_scores_arr[3].length;i++){
                        calculationCountScoreArr[i]=schedule_scores_arr[3][i][2];
                    }
                    let calculationSum = 0;
          
                    for (let i = 0; i < calculationCountScoreArr.length; i++){
                        calculationSum += parseInt(calculationCountScoreArr[i]);
                        }
                    data.list[0].calculationCountScore=calculationSum;
                }
                if(schedule_scores_arr[4].length==0){
               
                    data.list[0].applicationCountScore='0';
                }else{
                   
                    let applicationCountScoreArr=new Array();
                    for(let i=0;i<schedule_scores_arr[4].length;i++){
                        applicationCountScoreArr[i]=parseInt(schedule_scores_arr[4][i][2])+parseInt(schedule_scores_arr[4][i][3])+parseInt(schedule_scores_arr[4][i][4])+parseInt(schedule_scores_arr[4][i][5])+parseInt(schedule_scores_arr[4][i][6]);
                    }
                    let applicationSum = 0;
          
                    for (let i = 0; i < applicationCountScoreArr.length; i++){
                        applicationSum += parseInt(applicationCountScoreArr[i]);
                        }
                    data.list[0].applicationCountScore=applicationSum;
                }
                data.list[0].schedule_start=dateFormat(data.list[0].schedule_start, 'yyyy-mm-dd hh:mm:ss');
                data.list[0].schedule_end=dateFormat(data.list[0].schedule_end, 'yyyy-mm-dd hh:mm:ss');
                data.list[0].prac_dline=dateFormat(data.list[0].prac_dline, 'yyyy-mm-dd hh:mm:ss');
                data.list[0].test_dline=dateFormat(data.list[0].test_dline, 'yyyy-mm-dd hh:mm:ss');
                data.list[0].schedule_scores=schedule_scores_arr;
       
            }
           
            
            if(data) res.send(ApiResponse.ok(data));
        } catch (err) {
            next(err);
        }
    }
    async queryPoint(){
        const [req, res, next] = arguments;
        try {
       
            const data =[
                {id:'1',name:'知识点1'},
                {id:'2',name:'知识点2'},
                {id:'3',name:'知识点3'},
                {id:'4',name:'知识点4'},
            ];

            
            if(data) res.send(ApiResponse.ok(data));
        } catch (err) {
            next(err);
        }
    }
    async queryDifficulty(){
        const [req, res, next] = arguments;
        try {
       
            const data =[
                {id:'0',name:'简单'},
                {id:'1',name:'困难'},
               
            ];

            
            if(data) res.send(ApiResponse.ok(data));
        } catch (err) {
            next(err);
        }
    }
    async queryMethodologe(){
        const [req, res, next] = arguments;
        try {
       
            const data =[
                {id:'1',name:'方法论1'},
                {id:'2',name:'方法论2'},
                {id:'3',name:'方法论3'},
                {id:'4',name:'方法论4'},
            ];

            
            if(data) res.send(ApiResponse.ok(data));
        } catch (err) {
            next(err);
        }
    }

    // async delete() {
    //     const [req, res, next] = arguments;
    //     try {
    //         await this.service.dictionary.delete(req.params.id, true);
    //         res.send(ApiResponse.ok());
    //     } catch (err) {
    //         next(err);
    //     }
    // }
}